import { createRouter, createWebHistory } from 'vue-router'
import middle from '@/views/middle/index.vue'
import { defineAsyncComponent } from 'vue'; //异步组挂载器
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'middle',
      component: middle,
      meta:{
        title: "鉴权"
      }
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/login/index.vue'),
      meta:{
        title: "登录"
      }
    },
    {
      path: '/404',
      name: '404',
      component: () => import('../views/route_404/index.vue'),
      meta:{
        title: "404错误"
      }
    },
    {
      path: '/DataAdmin',
      name: 'DataAdmin',
      // @ts-ignore
      component: () => import('../views/DataAdmin/index.vue'),
      meta:{
        title: "大屏"
      }
    },
    {
      path: '/home',
      name: 'home',
      component: () => import('../views/home/index.vue'),
      meta:{
        title: "首页"
      },
      children:[
        {
          path: '/dashboard/dataAnalyse',
          name: 'dataAnalyse',
          meta: {title:'数据分析'},
          component: () => import('../views/dashboard/dataAnalyse/index.vue')
        },
        {
          path: '/root/rootGroup',
          name: 'rootGroup',
          meta: {title:'权限组管理'},
          component: () => import('../views/root/rootGroup/index.vue')
        },
        {
          path: '/root/userList',
          name: 'userList',
          meta: {title:'用户管理'},
          component: () => import('../views/root/userList/index.vue')
        },
        {
          path: '/hotel/index',
          name: 'hotel',
          meta: {title:'供应商管理'},
          component: () => import('../views/hotel/index.vue')
        },
        {
          path: '/Homeindex',
          name: 'Homeindex',
          component: () => import('../views/Homeindex/index.vue')
        },
        {
          path: '/AdminSetting',
          name: 'AdminSetting',
          meta: {title:'设置'},
          component: () => import('../views/AdminSetting/index.vue')
        },
        {
          path: '/getMoney',
          name: 'getMoney',
          meta: {title:'收银台'},
          component: () => import('../views/getMoney/index.vue')
        },
        {
          path: '/root/routeSet',
          name: 'routeSet',
          meta: {title:'路由管理'},
          component: () => import('../views/root/routeSet/index.vue')
        },
        {
          path: '/shopType',
          name: 'shopType',
          meta: {title:'产品分类'},
          component: () => import('../views/shopType/index.vue')
        },
        {
          path: '/UserInfo',
          name: 'UserInfo',
          meta: {title:'用户资料'},
          component: () => import('../views/UserInfo/index.vue')
        },
      ]
    }
  ]
})
import {storeVuex} from "@/store/modules/user";


//设置白名单
const noRoot_List = ['/middle','/404','/','/login','/DataAdmin']
// 路由守卫
//跳转前进行登录判断
router.beforeEach((to, from, next) => {

  const vuex = storeVuex()
  //设置文档title
  // @ts-ignore
  to.meta?.title && (document.title = to.meta.title)
  // 默认页跳转登录页
  if(to.path=='/'||to.path==''){
    router.push('/login')
  }
  //定义一个参数确认访问的页面是否在白名单
  const goPath = noRoot_List.includes(to.path as string)
  if(goPath){
    // 在免登录白名单，直接进入
    next()
  } else if(vuex.accessToken){
    // @ts-ignore
    let res = vuex.RootRoute.children.find((item:any)=>item.path ==to.path)
    // 如果访问的页面  没有权限访问
    if(!res){
      router.push('/404')
    }else{
      next()
    }
    console.log('是否有token',res)
  }
})
export default router
